//
//  HJNewsDailyCellLayout.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJNewsDailyLayout.h"
#import "HJAttributedStringFactory.h"

@interface HJNewsDailyLayout()

@property (nonatomic, strong, readwrite) HJInformationModel *model;

@end


@implementation HJNewsDailyLayout

//MJLogAllIvars


- (instancetype)init {
    if (self = [super init]) {
        self.isFold = YES;
    }
    return self;
}

+ (HJNewsDailyLayout *)layoutWithModel:(HJInformationModel *)model {
    HJNewsDailyLayout *layout = [[HJNewsDailyLayout alloc] init];
    layout.model = model;
    [layout calculateLayout];
    return layout;
}

- (void)calculateLayout {
    
    if (_model.images.count > 0) {
        _mediaDatas = _model.images;
        self -> _isVideo = NO;
    } else {
        _mediaDatas = _model.videos;
        self -> _isVideo = YES;
    }
    
    CGRect pointRect = CGRectMake(8, (18 - 6) * 0.5, 6, 6);
    
    if (_model.type == 2) {//广告
        if (_mediaDatas.count > 0){
            _imageRect = CGRectMake(0, 0, KScreenWidth - leftRightMargin * 2, ZOOM(122));
        } else {
            _imageRect = CGRectMake(0, 0, KScreenWidth - leftRightMargin * 2, 0);
        }
        _contentRect = CGRectMake(leftRightMargin, 0, _imageRect.size.width, _imageRect.size.height);
         _height = CGRectGetMaxY(_contentRect) + 15;
        _lineRect = CGRectMake(pointRect.origin.x + pointRect.size.width * 0.5, 0, 0.7, _height);
        return;
    }

    _pointRect = pointRect;
    _timeRect = CGRectMake(CGRectGetMaxX(_pointRect) + 6, 0, 150, 18);
    
    CGFloat viewY = 15;
    CGFloat viewX = 15;
    
    CGFloat maxW = KScreenWidth - leftRightMargin * 2 - viewX * 2;
    
    NSString *title = self.model.newsTitle;
    
    HJAttributedStringFactory *titleAttributedStringFactory = [[HJAttributedStringFactory alloc] initWithString:title maxWidth:maxW font:UIFontMediumMake(14) lineSpacing:3 alignment:NSTextAlignmentLeft];
    self.titleAttributedString = titleAttributedStringFactory.attributedString;
    CGSize titleSize = titleAttributedStringFactory.size;
    
    _titleRect = CGRectMake(viewX, viewY, titleSize.width, titleSize.height);
    
    HJAttributedStringFactory *contentAttributedStringFactory = [[HJAttributedStringFactory alloc] initWithString:self.model.content maxWidth:maxW font:UIFontMake(12) lineSpacing:3 alignment:NSTextAlignmentLeft];
    self.contentAttributedString = contentAttributedStringFactory.attributedString;
    
    CGSize textSize = contentAttributedStringFactory.size;
    if (contentAttributedStringFactory.numberOfLines > 3) {
        _isShowAllButton = YES;
        textSize.height = [contentAttributedStringFactory numberLineHeight:3];
        if (_isFold) {
            textSize.height = [contentAttributedStringFactory numberLineHeight:3];
        } else {
            textSize.height = contentAttributedStringFactory.size.height;
        }
    }
    
    if (_mediaDatas.count > 0){
        _imageRect = CGRectMake(viewX, CGRectGetMaxY(_titleRect) + 10, KScreenWidth - leftRightMargin * 2 - viewX * 2, ZOOM(122));
    } else {
        _imageRect = CGRectMake(viewX, CGRectGetMaxY(_titleRect), KScreenWidth - leftRightMargin * 2 - viewX * 2, 0);
    }
    
    if (_mediaDatas.count > 0){
        _textRect = CGRectMake(viewX, CGRectGetMaxY(_imageRect) + 8, textSize.width, textSize.height);
    } else {
        _textRect = CGRectMake(viewX, CGRectGetMaxY(_titleRect) + 10, textSize.width, textSize.height);
    }

    
    CGSize allButtonSize = [(_isFold ? @"All" : @"Conceal") getStringSizeWithWidth:maxW font:UIFontMake(12)];
    CGSize sourceLabelSize = [[NSString stringWithFormat:@"%@", self.model.source] getStringSizeWithWidth:maxW font:UIFontMake(12)];
        
    if (_isShowAllButton) {
        _foldButtonRect =  CGRectMake(viewX, CGRectGetMaxY(_textRect) + 4, allButtonSize.width + 20, allButtonSize.height);
        _sourceRect = CGRectMake(viewX,  CGRectGetMaxY(_foldButtonRect) + 10, sourceLabelSize.width, sourceLabelSize.height);
    } else {
        _foldButtonRect =  CGRectMake(viewX, CGRectGetMaxY(_textRect), allButtonSize.width, allButtonSize.height);
        _sourceRect = CGRectMake(viewX,  CGRectGetMaxY(_textRect) + 10, sourceLabelSize.width, sourceLabelSize.height);
    }
    
    _operateRect = CGRectMake(viewX, CGRectGetMaxY(_sourceRect) + 12, KScreenWidth - leftRightMargin * 2, 32);
    _contentRect = CGRectMake(leftRightMargin, CGRectGetMaxY(_timeRect) + 10, KScreenWidth - leftRightMargin * 2, CGRectGetMaxY(_operateRect) + 10);
    
    _height = CGRectGetMaxY(_contentRect) + 15;
    
    if (_model.index == 0) {
        _lineRect = CGRectMake(_pointRect.origin.x + _pointRect.size.width * 0.5, CGRectGetMaxY(_pointRect), 0.7, _height - CGRectGetMaxY(_pointRect));
    } else {
        _lineRect = CGRectMake(_pointRect.origin.x + _pointRect.size.width * 0.5, 0, 0.7, _height);
    }
    
}


@end
